package com.perfma.xtest.sql;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: junhui.si
 * @date: 2020-07-13 18:16
 **/
public class SqlParseTest {
    public static void main(String[] args) {
        String dbType = JdbcConstants.MYSQL;
//        String sqlT = "select sname,st.sno,scn.cn from student st join (select distinct sno,count(*)cn from sc group by sc.sno)scn on(st.sno=scn.sno) where scn.cn=(select distinct count(cno) from course)";
        String sqlT = "SELECT ID, NAME, AGE FROM USER WHERE ID = ?";
//        String sqlTemplate = "alter table ddDDAAd add index idx_qw(q,w)";
        List<String> sqlList = new ArrayList<>();
        for (int i = 0; i < 5000; i++) {
            sqlList.add(sqlT.replace("student", "student" + i).replace("sc", "sc" + i));
        }

        SchemaStatVisitor visitor1;
        List<SQLStatement> sqlStatements1 = SQLUtils.toStatementList(sqlT, dbType);
        for (SQLStatement sqlStatement : sqlStatements1) {
            visitor1 = new SchemaStatVisitor(dbType);
            sqlStatement.accept(visitor1);
            System.out.println(visitor1.getTables());
        }

        long startTime = System.currentTimeMillis();
//        for (String sql : sqlList) {
        for (int i = 0; i < 10000*10 ; i++) {
            long startTime1 = System.nanoTime();
            SchemaStatVisitor visitor;
            List<SQLStatement> sqlStatements = SQLUtils.toStatementList(sqlT, dbType);
            for (SQLStatement sqlStatement : sqlStatements) {
                visitor = new SchemaStatVisitor(dbType);
                sqlStatement.accept(visitor);
                visitor.getTables();
            }
//            System.out.println((System.nanoTime() - startTime1) + " ns");
        }
        System.out.println(System.currentTimeMillis() - startTime);

//        perfMySql(sqlT);
    }

    static long perfMySql(String sql) {
        long startYGC = TestUtils.getYoungGC();
        long startYGCTime = TestUtils.getYoungGCTime();
        long startFGC = TestUtils.getFullGC();

        long startMillis = System.currentTimeMillis();
        for (int i = 0; i < 1000 * 1000; ++i) {
            execMySql(sql);
        }
        long millis = System.currentTimeMillis() - startMillis;

        long ygc = TestUtils.getYoungGC() - startYGC;
        long ygct = TestUtils.getYoungGCTime() - startYGCTime;
        long fgc = TestUtils.getFullGC() - startFGC;

        System.out.println("MySql\t" + millis + ", ygc " + ygc + ", ygct " + ygct + ", fgc " + fgc);
        return millis;
    }

    private static String execMySql(String sql) {
        StringBuilder out = new StringBuilder();
        MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        List<SQLStatement> statementList = parser.parseStatementList();
        // for (SQLStatement statement : statementList) {
        // statement.accept(visitor);
        // visitor.println();
        // }
        return out.toString();
    }
}
